﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Media.Imaging;
using System.Windows.Input;

namespace STEditor.Classes
{
    public class STGame : NotifyObjectBase
    {

        public ObservableCollection<STGameArea> GameAreas { get; private set; }
        public STTileImageCollection TileTypes { get; private set; }

        private STGameArea _currentArea;
        public STGameArea CurrentArea
        {
            get { return _currentArea; }
            set
            {
                _currentArea = value;
                RaisePropertyChanged("CurrentArea");
            }
        }

        private bool _fillMode = false;
        public bool FillMode
        {
            get { return _fillMode; }
            set
            {
                _fillMode = value;
                RaisePropertyChanged("FillMode");
            }
        }

        public STGame()
        {

            GameAreas = new ObservableCollection<STGameArea>();
            GameAreas.CollectionChanged += GameAreas_CollectionChanged;
            TileTypes = new STTileImageCollection();
            TileTypes.Add(EmptyTile.EmptyImage);

        }

        void GameAreas_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {

            if (GameAreas.Count == 1)
            {
                CurrentArea = GameAreas.First();
            }

            if (e.NewItems != null)
            {
                foreach (STGameArea area in e.NewItems)
                {
                    area.Game = this;
                }
            }

            if (e.OldItems != null)
            {
                foreach (STGameArea area in e.OldItems)
                {
                    area.Game = null;
                }
            }

        }

        public ICommand AddNewAreaCommand
        {
            get { return new AddNewAreaCmd(this); }
        }

        public void AddNewArea()
        {
            string areaName = "Area" + (GameAreas.Count + 1);
            var newArea = new STGameArea { TileWidth = 32, TileHeight = 32, Width = 12, Height = 8, AreaName = areaName };
            GameAreas.Add(newArea);
        }

        public void Save(string fileName)
        {

            var ser = new System.Xml.Serialization.XmlSerializer(typeof(STGame));
            
            using (var fs = System.IO.File.OpenWrite(fileName))
            {
                ser.Serialize(fs, this);
            }

        }

        public class AddNewAreaCmd : ICommand
        {

            private STGame _game;
            public AddNewAreaCmd(STGame game)
            {
                _game = game;
            }

            public bool CanExecute(object parameter)
            {
                return true;
            }

            public event EventHandler CanExecuteChanged;

            public void Execute(object parameter)
            {
                _game.AddNewArea();
            }

        }

    }
}
